local description = [[
# 测试模式

所有玩家都需要为自己而战！既要谋略，亦要实力，不再受后置位坐牢之苦，好好发挥出你的真实水准吧。

规则如下：

- 胜利条件为击杀所有其他角色，成为唯一的生存者。
- 击杀后摸2张牌。
- 采用常规回合流程，按顺序轮流行动。

]]

local test_getLogic = function()
  local test_logic = GameLogic:subclass("test_logic")

  function test_logic:assignRoles()

    local function getOtherPlayers(room, p)
      local ret = table.simpleClone(room.players)
      table.removeOne(ret, p)
      return ret
    end

    local function getPlayerByScreenName(room, name)
      return table.find(room.players, function(p)
        return p._splayer:getScreenName() == name
      end)
    end

    local room = self.room
    local croom = room.room
    local cowner = croom:getOwner()
    local owner = getPlayerByScreenName(room, cowner:getScreenName())
    local others = getOtherPlayers(room, owner)

    local n = #room.players

    for i = 1, n do
      local p = room.players[i]
      p.role = "hidden"  -- 隐藏身份
      room:setPlayerProperty(p, "role_shown", true)
      room:broadcastProperty(p, "role")
    end
    local players = {}
    players[1] = owner
    players[2] = others[1]
    room.players = players
    self.start_role = "hidden"
    -- 为了座位调整兼容，临时设置第一个玩家为主公标识（不影响实际逻辑）
    room.players[1].role = "lord"
  end

  function test_logic:chooseGenerals()
    local room = self.room
    local generalNum = room.settings.generalNum
    local n = room.settings.enableDeputy and 2 or 1  -- 是否启用副将
    local lord = room:getLord()
    room:setCurrent(lord)
    lord.role = self.start_role

    local players = room.players
    local generals = room:getNGenerals(#players * generalNum)
    local req = Request:new(players, "AskForGeneral")
    table.shuffle(generals)  -- 打乱武将顺序

    -- 分配武将选择池
    for i, p in ipairs(players) do
      local arg = table.slice(generals, (i - 1) * generalNum + 1, i * generalNum)
      req:setData(p, { arg, n })
      req:setDefaultReply(p, table.random(arg, n))
    end

    req:ask()  -- 发送选择请求

    -- 处理选择结果
    local selected = {}
    for _, p in ipairs(players) do
      local gs = req:getResult(p)
      local general = gs[1]
      local deputy = gs[2]
      room:setPlayerGeneral(p, general, true, true)
      if deputy then
        room:setDeputyGeneral(p, deputy)
      end
      table.insert(selected, general)
      if deputy then table.insert(selected, deputy) end
    end

    -- 归还未被选择的武将
    local remaining = table.filter(generals, function(g)
      return not table.contains(selected, g)
    end)
    room:returnToGeneralPile(remaining)

    -- 选择势力
    room:askToChooseKingdom(players)
  end

  return test_logic
end

local test_mode = fk.CreateGameMode{
  name = "test_mode",
  minPlayer = 2,
  maxPlayer = 2,
  logic = test_getLogic,  -- 移除了混战规则技能依赖

  -- 投降判定：只剩两人时可投降
  surrender_func = function(self, playedTime)
    local alive = table.filter(Fk:currentRoom().players, function(p)
      return p.rest > 0 or not p.dead
    end)
    return { { text = "测试模式：仅剩两人时可投降", passed = #alive == 2 } }
  end,

  -- 击杀奖励：摸2张牌
  reward_punish = function (self, victim, killer)
    if killer and not killer.dead then
      killer:drawCards(0, "击杀奖励")
    end
  end,

  -- 胜利判定：唯一存活者获胜
  winner_getter = function(self, victim)
    -- 若受害者未投降且还有体力，不判定胜利
    if not victim.surrendered and victim.rest > 0 then
      return ""
    end

    local room = victim.room
    local alive = table.filter(room.players, function(p)
      return not p.surrendered and not (p.dead and p.rest == 0)
    end)

    -- 若只剩一人，判定其胜利
    if #alive == 1 then
      local winner = alive[1]
      winner.role = "renegade"  -- 用叛逆标识唯一胜利者（不影响实际逻辑）
      if room.class.name == 'Room' then
        room:broadcastProperty(winner, 'role')
      end
      return "renegade"
    end

    return ""
  end,
}

-- 加载翻译文本
Fk:loadTranslationTable{
  ["test_mode"] = "测试模式",
  [":test_mode"] = description,
}

return test_mode